var createError = require("http-errors");
var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");

var userRouter = require("./routes/user");
var classRouter = require("./routes/class");
var newsRouter = require("./routes/news");
var expRouter = require("./routes/exp");
var courseRouter = require("./routes/course");

var uploadWord = require("./routes/upload/wordfile");
var uploadAvatar = require("./routes/upload/avatarfile");
var uploadFj = require("./routes/upload/fjfile");
var uploadQt = require("./routes/upload/qt");

var app = express();

// app.all("*", function (req, res, next) {
//   setTimeout(function () {
//     next();
//   }, 120000);
// });
// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "jade");

// app.use(function (req, res, next) {
//   // 这里必须是Response响应的定时器【120秒】
//   res.setTimeout(120 * 1000, function () {
//     console.log("Request has timed out.");
//     return res.status(408).send("请求超时");
//   });
//   next();
// });

app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use("/file", express.static(path.join(__dirname, "public")));

// app.use('/', indexRouter);
app.use("/api/user", userRouter);
app.use("/api/class", classRouter);
app.use("/api/news", newsRouter);
app.use("/api/exp", expRouter);
app.use("/api/course", courseRouter);

app.use("/api/up_word", uploadWord);
app.use("/api/up_fj", uploadFj);
app.use("/api/up_avatar", uploadAvatar);
app.use("/api/up_qt", uploadQt);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get("env") === "development" ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render("error");
});

module.exports = app;
